﻿namespace KeRui.DAL
{
    using KeRui.BLL;
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class Seller
    {
        public int Add(KeRui.Model.Seller model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@SellerID", SqlDbType.Int, 4), new SqlParameter("@SellerName", SqlDbType.NVarChar, 50), new SqlParameter("@SellerType", SqlDbType.Int, 4), new SqlParameter("@VocationID", SqlDbType.Int, 4), new SqlParameter("@VocationName", SqlDbType.NVarChar, 50), new SqlParameter("@isExtend", SqlDbType.Bit, 1), new SqlParameter("@OrderID", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@UserName", SqlDbType.NVarChar, 20), new SqlParameter("@AppID", SqlDbType.Int, 4), new SqlParameter("@AppName", SqlDbType.NVarChar, 500), new SqlParameter("@TypeID", SqlDbType.Int, 4), new SqlParameter("@TypeName", SqlDbType.NVarChar, 500), new SqlParameter("@SellerSN", SqlDbType.VarChar, 0xff), new SqlParameter("@SellerSNImage", SqlDbType.VarChar, 0xff), new SqlParameter("@Province", SqlDbType.NVarChar, 50), 
                new SqlParameter("@City", SqlDbType.NVarChar, 50), new SqlParameter("@Area", SqlDbType.NVarChar, 50), new SqlParameter("@Address", SqlDbType.NVarChar, 500), new SqlParameter("@Zip", SqlDbType.VarChar, 10), new SqlParameter("@Map", SqlDbType.VarChar, 50), new SqlParameter("@Tel", SqlDbType.VarChar, 50), new SqlParameter("@Mobile", SqlDbType.VarChar, 50), new SqlParameter("@Fax", SqlDbType.VarChar, 50), new SqlParameter("@Notice", SqlDbType.NVarChar, 500), new SqlParameter("@Images", SqlDbType.VarChar), new SqlParameter("@Homepage", SqlDbType.VarChar, 0xff), new SqlParameter("@Email", SqlDbType.VarChar, 50), new SqlParameter("@Views", SqlDbType.Int, 4), new SqlParameter("@State", SqlDbType.Int, 4), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15), 
                new SqlParameter("@Guid", SqlDbType.VarChar, 0x24), new SqlParameter("@SellBrand", SqlDbType.NVarChar, 50), new SqlParameter("@SellScope", SqlDbType.NVarChar, 50), new SqlParameter("@MobileCode", SqlDbType.Int, 4), new SqlParameter("@MapAbcX", SqlDbType.VarChar, 50), new SqlParameter("@MapAbcY", SqlDbType.VarChar, 50), new SqlParameter("@logo", SqlDbType.VarChar, 200), new SqlParameter("@Css", SqlDbType.VarChar, 200), new SqlParameter("@banner", SqlDbType.VarChar, 200), new SqlParameter("@TemplateFileID", SqlDbType.Int, 4), new SqlParameter("@RealName", SqlDbType.VarChar, 50), new SqlParameter("@ViewsStyle", SqlDbType.NVarChar), new SqlParameter("@Scores", SqlDbType.Int, 4), new SqlParameter("@UpdateTime", SqlDbType.DateTime), new SqlParameter("@SellerCost", SqlDbType.Int, 4)
             };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.SellerName;
            parameters[2].Value = model.SellerType;
            parameters[3].Value = model.VocationID;
            parameters[4].Value = model.VocationName;
            parameters[5].Value = model.isExtend;
            parameters[6].Value = model.OrderID;
            parameters[7].Value = model.UserID;
            parameters[8].Value = model.UserName;
            parameters[9].Value = model.AppID;
            parameters[10].Value = model.AppName;
            parameters[11].Value = model.TypeID;
            parameters[12].Value = model.TypeName;
            parameters[13].Value = model.SellerSN;
            parameters[14].Value = model.SellerSNImage;
            parameters[15].Value = model.Province;
            parameters[0x10].Value = model.City;
            parameters[0x11].Value = model.Area;
            parameters[0x12].Value = model.Address;
            parameters[0x13].Value = model.Zip;
            parameters[20].Value = model.Map;
            parameters[0x15].Value = model.Tel;
            parameters[0x16].Value = model.Mobile;
            parameters[0x17].Value = model.Fax;
            parameters[0x18].Value = model.Notice;
            parameters[0x19].Value = model.Images;
            parameters[0x1a].Value = model.Homepage;
            parameters[0x1b].Value = model.Email;
            parameters[0x1c].Value = model.Views;
            parameters[0x1d].Value = model.State;
            parameters[30].Value = model.CreateTime;
            parameters[0x1f].Value = model.CreateIP;
            parameters[0x20].Value = model.Guid;
            parameters[0x21].Value = model.SellBrand;
            parameters[0x22].Value = model.SellScope;
            parameters[0x23].Value = model.MobileCode;
            parameters[0x24].Value = model.MapAbcX;
            parameters[0x25].Value = model.MapAbcY;
            parameters[0x26].Value = model.logo;
            parameters[0x27].Value = model.Css;
            parameters[40].Value = model.banner;
            parameters[0x29].Value = model.TemplateFileID;
            parameters[0x2a].Value = model.RealName;
            parameters[0x2b].Value = model.ViewsStyle;
            parameters[0x2c].Value = model.Scores;
            parameters[0x2d].Value = model.UpdateTime;
            parameters[0x2e].Value = model.SellerCost;
            DbHelperSQL.RunProcedure("Seller_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public int AddGeneralSeller(KeRui.Model.Account modelAccount, int strSellerType)
        {
            int num;
            KeRui.BLL.Account account = new KeRui.BLL.Account();
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Username", SqlDbType.NVarChar, 20), new SqlParameter("@Password", SqlDbType.VarChar, 50), new SqlParameter("@SellerType", SqlDbType.Int), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15) };
            parameters[0].Value = modelAccount.Username;
            parameters[1].Value = modelAccount.Password;
            parameters[2].Value = strSellerType;
            parameters[3].Value = modelAccount.CreateIP;
            return DbHelperSQL.RunProcedure("Member_AddGeneralMember", parameters, out num);
        }

        public void Delete(int SellerID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SellerID", SqlDbType.Int, 4) };
            parameters[0].Value = SellerID;
            DbHelperSQL.RunProcedure("Seller_Delete", parameters, out num);
        }

        public bool Exists(int SellerID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SellerID", SqlDbType.Int, 4) };
            parameters[0].Value = SellerID;
            return (DbHelperSQL.RunProcedure("Seller_Exists", parameters, out num) == 1);
        }

        public DataSet GetListForTag(string strWhere)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@StrWhere", SqlDbType.NText) };
            parameters[0].Value = strWhere;
            return DbHelperSQL.RunProcedure("Seller_GetDataSetForTag", parameters, "ds");
        }

        public KeRui.Model.Seller GetModel(int SellerID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SellerID", SqlDbType.Int, 4) };
            parameters[0].Value = SellerID;
            KeRui.Model.Seller seller = new KeRui.Model.Seller();
            DataSet set = DbHelperSQL.RunProcedure("Seller_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["SellerID"].ToString() != "")
                {
                    seller.SellerID = int.Parse(set.Tables[0].Rows[0]["SellerID"].ToString());
                }
                seller.SellerName = set.Tables[0].Rows[0]["SellerName"].ToString();
                if (set.Tables[0].Rows[0]["SellerType"].ToString() != "")
                {
                    seller.SellerType = int.Parse(set.Tables[0].Rows[0]["SellerType"].ToString());
                }
                if (set.Tables[0].Rows[0]["VocationID"].ToString() != "")
                {
                    seller.VocationID = int.Parse(set.Tables[0].Rows[0]["VocationID"].ToString());
                }
                seller.VocationName = set.Tables[0].Rows[0]["VocationName"].ToString();
                if (set.Tables[0].Rows[0]["isExtend"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["isExtend"].ToString() == "1") || (set.Tables[0].Rows[0]["isExtend"].ToString().ToLower() == "true"))
                    {
                        seller.isExtend = true;
                    }
                    else
                    {
                        seller.isExtend = false;
                    }
                }
                if (set.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    seller.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
                }
                if (set.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    seller.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
                }
                seller.UserName = set.Tables[0].Rows[0]["UserName"].ToString();
                if (set.Tables[0].Rows[0]["AppID"].ToString() != "")
                {
                    seller.AppID = int.Parse(set.Tables[0].Rows[0]["AppID"].ToString());
                }
                seller.AppName = set.Tables[0].Rows[0]["AppName"].ToString();
                if (set.Tables[0].Rows[0]["TypeID"].ToString() != "")
                {
                    seller.TypeID = int.Parse(set.Tables[0].Rows[0]["TypeID"].ToString());
                }
                seller.TypeName = set.Tables[0].Rows[0]["TypeName"].ToString();
                seller.SellerSN = set.Tables[0].Rows[0]["SellerSN"].ToString();
                seller.SellerSNImage = set.Tables[0].Rows[0]["SellerSNImage"].ToString();
                seller.Province = set.Tables[0].Rows[0]["Province"].ToString();
                seller.City = set.Tables[0].Rows[0]["City"].ToString();
                seller.Area = set.Tables[0].Rows[0]["Area"].ToString();
                seller.Address = set.Tables[0].Rows[0]["Address"].ToString();
                seller.Zip = set.Tables[0].Rows[0]["Zip"].ToString();
                seller.Map = set.Tables[0].Rows[0]["Map"].ToString();
                seller.Tel = set.Tables[0].Rows[0]["Tel"].ToString();
                seller.Mobile = set.Tables[0].Rows[0]["Mobile"].ToString();
                seller.Fax = set.Tables[0].Rows[0]["Fax"].ToString();
                seller.Notice = set.Tables[0].Rows[0]["Notice"].ToString();
                seller.Images = set.Tables[0].Rows[0]["Images"].ToString();
                seller.Homepage = set.Tables[0].Rows[0]["Homepage"].ToString();
                seller.Email = set.Tables[0].Rows[0]["Email"].ToString();
                if (set.Tables[0].Rows[0]["Views"].ToString() != "")
                {
                    seller.Views = int.Parse(set.Tables[0].Rows[0]["Views"].ToString());
                }
                if (set.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    seller.State = int.Parse(set.Tables[0].Rows[0]["State"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    seller.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                seller.CreateIP = set.Tables[0].Rows[0]["CreateIP"].ToString();
                seller.Guid = set.Tables[0].Rows[0]["Guid"].ToString();
                seller.SellBrand = set.Tables[0].Rows[0]["SellBrand"].ToString();
                seller.SellScope = set.Tables[0].Rows[0]["SellScope"].ToString();
                if (set.Tables[0].Rows[0]["MobileCode"].ToString() != "")
                {
                    seller.MobileCode = int.Parse(set.Tables[0].Rows[0]["MobileCode"].ToString());
                }
                seller.MapAbcX = set.Tables[0].Rows[0]["MapAbcX"].ToString();
                seller.MapAbcY = set.Tables[0].Rows[0]["MapAbcY"].ToString();
                seller.logo = set.Tables[0].Rows[0]["logo"].ToString();
                seller.Css = set.Tables[0].Rows[0]["Css"].ToString();
                seller.banner = set.Tables[0].Rows[0]["banner"].ToString();
                if (set.Tables[0].Rows[0]["TemplateFileID"].ToString() != "")
                {
                    seller.TemplateFileID = int.Parse(set.Tables[0].Rows[0]["TemplateFileID"].ToString());
                }
                seller.RealName = set.Tables[0].Rows[0]["RealName"].ToString();
                seller.ViewsStyle = set.Tables[0].Rows[0]["ViewsStyle"].ToString();
                if (set.Tables[0].Rows[0]["Scores"].ToString() != "")
                {
                    seller.Scores = int.Parse(set.Tables[0].Rows[0]["Scores"].ToString());
                }
                if (set.Tables[0].Rows[0]["UpdateTime"].ToString() != "")
                {
                    seller.UpdateTime = DateTime.Parse(set.Tables[0].Rows[0]["UpdateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["SellerCost"].ToString() != "")
                {
                    seller.SellerCost = int.Parse(set.Tables[0].Rows[0]["SellerCost"].ToString());
                }
                return seller;
            }
            return null;
        }

        public int GetSellerIDByUserid(int userID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", SqlDbType.Int, 4) };
            parameters[0].Value = userID;
            return DbHelperSQL.RunProcedure("Seller_GetIdByUserid", parameters, out num);
        }

        public DataSet GetSetModel(int SellerID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SellerID", SqlDbType.Int, 4) };
            parameters[0].Value = SellerID;
            KeRui.Model.Seller seller = new KeRui.Model.Seller();
            return DbHelperSQL.RunProcedure("Seller_GetModel", parameters, "ds");
        }

        public void Update(KeRui.Model.Seller model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@SellerID", SqlDbType.Int, 4), new SqlParameter("@SellerName", SqlDbType.NVarChar, 50), new SqlParameter("@SellerType", SqlDbType.Int, 4), new SqlParameter("@VocationID", SqlDbType.Int, 4), new SqlParameter("@VocationName", SqlDbType.NVarChar, 50), new SqlParameter("@isExtend", SqlDbType.Bit, 1), new SqlParameter("@OrderID", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@UserName", SqlDbType.NVarChar, 20), new SqlParameter("@AppID", SqlDbType.Int, 4), new SqlParameter("@AppName", SqlDbType.NVarChar, 500), new SqlParameter("@TypeID", SqlDbType.Int, 4), new SqlParameter("@TypeName", SqlDbType.NVarChar, 500), new SqlParameter("@SellerSN", SqlDbType.VarChar, 0xff), new SqlParameter("@SellerSNImage", SqlDbType.VarChar, 0xff), new SqlParameter("@Province", SqlDbType.NVarChar, 50), 
                new SqlParameter("@City", SqlDbType.NVarChar, 50), new SqlParameter("@Area", SqlDbType.NVarChar, 50), new SqlParameter("@Address", SqlDbType.NVarChar, 500), new SqlParameter("@Zip", SqlDbType.VarChar, 10), new SqlParameter("@Map", SqlDbType.VarChar, 50), new SqlParameter("@Tel", SqlDbType.VarChar, 50), new SqlParameter("@Mobile", SqlDbType.VarChar, 50), new SqlParameter("@Fax", SqlDbType.VarChar, 50), new SqlParameter("@Notice", SqlDbType.NVarChar, 500), new SqlParameter("@Images", SqlDbType.VarChar), new SqlParameter("@Homepage", SqlDbType.VarChar, 0xff), new SqlParameter("@Email", SqlDbType.VarChar, 50), new SqlParameter("@Views", SqlDbType.Int, 4), new SqlParameter("@State", SqlDbType.Int, 4), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15), 
                new SqlParameter("@Guid", SqlDbType.VarChar, 0x24), new SqlParameter("@SellBrand", SqlDbType.NVarChar, 50), new SqlParameter("@SellScope", SqlDbType.NVarChar, 50), new SqlParameter("@MobileCode", SqlDbType.Int, 4), new SqlParameter("@MapAbcX", SqlDbType.VarChar, 50), new SqlParameter("@MapAbcY", SqlDbType.VarChar, 50), new SqlParameter("@logo", SqlDbType.VarChar, 200), new SqlParameter("@Css", SqlDbType.VarChar, 200), new SqlParameter("@banner", SqlDbType.VarChar, 200), new SqlParameter("@TemplateFileID", SqlDbType.Int, 4), new SqlParameter("@RealName", SqlDbType.VarChar, 50), new SqlParameter("@ViewsStyle", SqlDbType.NVarChar), new SqlParameter("@Scores", SqlDbType.Int, 4), new SqlParameter("@UpdateTime", SqlDbType.DateTime), new SqlParameter("@SellerCost", SqlDbType.Int, 4)
             };
            parameters[0].Value = model.SellerID;
            parameters[1].Value = model.SellerName;
            parameters[2].Value = model.SellerType;
            parameters[3].Value = model.VocationID;
            parameters[4].Value = model.VocationName;
            parameters[5].Value = model.isExtend;
            parameters[6].Value = model.OrderID;
            parameters[7].Value = model.UserID;
            parameters[8].Value = model.UserName;
            parameters[9].Value = model.AppID;
            parameters[10].Value = model.AppName;
            parameters[11].Value = model.TypeID;
            parameters[12].Value = model.TypeName;
            parameters[13].Value = model.SellerSN;
            parameters[14].Value = model.SellerSNImage;
            parameters[15].Value = model.Province;
            parameters[0x10].Value = model.City;
            parameters[0x11].Value = model.Area;
            parameters[0x12].Value = model.Address;
            parameters[0x13].Value = model.Zip;
            parameters[20].Value = model.Map;
            parameters[0x15].Value = model.Tel;
            parameters[0x16].Value = model.Mobile;
            parameters[0x17].Value = model.Fax;
            parameters[0x18].Value = model.Notice;
            parameters[0x19].Value = model.Images;
            parameters[0x1a].Value = model.Homepage;
            parameters[0x1b].Value = model.Email;
            parameters[0x1c].Value = model.Views;
            parameters[0x1d].Value = model.State;
            parameters[30].Value = model.CreateTime;
            parameters[0x1f].Value = model.CreateIP;
            parameters[0x20].Value = model.Guid;
            parameters[0x21].Value = model.SellBrand;
            parameters[0x22].Value = model.SellScope;
            parameters[0x23].Value = model.MobileCode;
            parameters[0x24].Value = model.MapAbcX;
            parameters[0x25].Value = model.MapAbcY;
            parameters[0x26].Value = model.logo;
            parameters[0x27].Value = model.Css;
            parameters[40].Value = model.banner;
            parameters[0x29].Value = model.TemplateFileID;
            parameters[0x2a].Value = model.RealName;
            parameters[0x2b].Value = model.ViewsStyle;
            parameters[0x2c].Value = model.Scores;
            parameters[0x2d].Value = model.UpdateTime;
            parameters[0x2e].Value = model.SellerCost;
            DbHelperSQL.RunProcedure("Seller_Update", parameters, out num);
        }

        public int UpdateBasicInfo(KeRui.Model.Seller model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@SellerID", SqlDbType.Int, 4), new SqlParameter("@SellerSN", SqlDbType.VarChar, 0xff), new SqlParameter("@SellerSNImage", SqlDbType.VarChar, 0xff), new SqlParameter("@Province", SqlDbType.NVarChar, 50), new SqlParameter("@City", SqlDbType.NVarChar, 50), new SqlParameter("@Area", SqlDbType.NVarChar, 50), new SqlParameter("@Address", SqlDbType.NVarChar, 50), new SqlParameter("@Zip", SqlDbType.VarChar, 10), new SqlParameter("@Tel", SqlDbType.VarChar, 50), new SqlParameter("@Mobile", SqlDbType.VarChar, 50), new SqlParameter("@Fax", SqlDbType.VarChar, 50), new SqlParameter("@Email", SqlDbType.VarChar, 50), new SqlParameter("@Images", SqlDbType.VarChar, 0xff) };
            parameters[0].Value = model.SellerID;
            parameters[1].Value = model.SellerSN;
            parameters[2].Value = model.SellerSNImage;
            parameters[3].Value = model.Province;
            parameters[4].Value = model.City;
            parameters[5].Value = model.Area;
            parameters[6].Value = model.Address;
            parameters[7].Value = model.Zip;
            parameters[8].Value = model.Tel;
            parameters[9].Value = model.Mobile;
            parameters[10].Value = model.Fax;
            parameters[11].Value = model.Email;
            parameters[12].Value = model.Images;
            DbHelperSQL.RunProcedure("Seller_UpdateBasicInfo", parameters, out num);
            return num;
        }
    }
}

